#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

struct node
{
	int data;
	struct node *next;
}*p;

void main()
{
	p=NULL;
	
	int s,i;
	printf("\n enter size of list \t :");
	scanf("%d",&s);



	struct node *temp;



	for(i=0;i<s;i++)
	{
		
	        temp=malloc(sizeof(struct node));
		printf("\n enter the no. to insert   :");
		scanf("%d",&temp->data);

		temp->next=p;
		p=temp;
		
	}

	for(i=0;i<s;i++)
	{
		printf(" %d",p->data);
		p=p->next;
	}
	int c;
	printf("enter your choice:\n\n");
	printf(" 1.Insert \n 2.Count \n 3.Index");
	switch(c)
	{	
	case 1:
		int z,l;
		printf("enter the no. to insert");
		scanf(" %d",&z);
		printf(" enter the location of to insert no.");
		scanf(" %d",&l);
		insert(z,l,s);
		break;
	case 2:	count();
		break;
	case 3: int num;
		printf("enter the no. to append:");
		scanf(" %d",&num); 
		append(num);
	case 4: reverse(temp);
	case 5: exit();
	}

insert(int z,int l,int s)
{
	int i;
	struct node *r,*t,*tempr;
	r=p;
	if(l>s)
	{
		printf("Index ented greater than size : NO INSERTION \n\n");
	}
	else
	{
		for(i=1;i<l;i++)
		{
			t=r;   // t will be holding previous value 
			r=r->next;
		}
	        tempr=(struct node *)malloc(sizeof(struct node));
	        tempr->data=z;
	        t->next=tempr;
	        t=tempr;
	        t->next=r;		

	}
	for(i=0;i<s;i++)
	{
		printf(" %d",p->data);
		p=p->next;
	}

}
count()
{

	struct node *n;
	int c=0;	
	n=p;
	while(n!=NULL)
	{
		n=n->next;
		c++;
	}
	printf(" the length is: %d",c)
 }

append( int num )
 {
     struct node *temp,*r;
      temp= (struct node *)malloc(sizeof(struct node));
       temp->data=num;
       r=(struct node *)p;

      if (p == NULL) 
     {
       p=temp;
         p->next =NULL;
     }
  else
     {        

       		while( r->next != NULL)
       r=r->next;
       r->next =temp;
       r=temp;
       r->next=NULL;
     }
}

reverse(struct node *q)
{
   struct node *m, *n,*l,*s;
   m=q;
   n=NULL;
  while(m!=NULL)
 {
   s=n;
   n=m;
  m=m->next;
  n->next=s;
 }
   p=n;
}

